<template>
  <a-form :model="form" label-align="left">
    <a-form-item field="title" label="标题">
      <a-input v-model="form.title" placeholder="请输入标题" />
    </a-form-item>
    <a-form-item label="标签">
      <a-input-tag
        v-model="form.tags"
        :style="{ width: '320px' }"
        placeholder="请输入标签"
        allow-clear
      />
    </a-form-item>
    <a-form-item field="content" label="题目内容">
      <MdEditor :value="form.content" :handle-change="onChange2" />
    </a-form-item>
    <a-form-item field="answer" label="答案">
      <MdEditor :value="form.answer" :handle-change="onChange1" />
    </a-form-item>
    <a-form-item label="判题配置" :content-flex="false" :merge-props="false">
      <a-space direction="vertical">
        <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
          <a-input-number
            v-model="form.judgeConfig.memoryLimit"
            :style="{ width: '320px' }"
            placeholder="请输入内存限制"
            :min="0"
          />
        </a-form-item>
        <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
          <a-input-number
            v-model="form.judgeConfig.stackLimit"
            :style="{ width: '320px' }"
            placeholder="请输入堆栈限制"
            :min="0"
          />
        </a-form-item>
        <a-form-item field="judgeConfig.timeLimit" label="时间限制">
          <a-input-number
            v-model="form.judgeConfig.timeLimit"
            :style="{ width: '320px' }"
            placeholder="请输入时间限制"
            :min="0"
          />
        </a-form-item>
      </a-space>
    </a-form-item>

    <a-form-item
      v-for="(judgeCaseItem, index) of form.judgeCase"
      :key="index"
      label="测试用例配置"
    >
      <a-space direction="vertical">
        <a-form-item
          :field="`judgeCaseItem[${index}].input`"
          :label="`输入用例-${index}`"
          style="min-width: 480px"
        >
          <a-input v-model="judgeCaseItem.input" placeholder="请输入输入用例" />
        </a-form-item>
        <a-form-item
          :field="`judgeCaseItem[${index}].output`"
          :label="`输出用例-${index}`"
          style="min-width: 480px"
        >
          <a-input
            v-model="judgeCaseItem.output"
            placeholder="请输入输出用例"
          />
        </a-form-item>
        <a-button
          @click="handleDelete(index)"
          :style="{ marginLeft: '10px' }"
          status="danger"
          >删除
        </a-button>
      </a-space>
    </a-form-item>
    <div>
      <a-button status="success" type="dashed" @click="handleAdd"
        >新增测试用例
      </a-button>
    </div>

    <a-form-item>
      <a-button type="primary" style="width: 200px" @click="doSubmit"
        >提交
      </a-button>
    </a-form-item>
  </a-form>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

const route = useRoute();
const updatePage = route.path.includes("update");
// 如果是要更新数据就加载数据
const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as never
  );
  if (res.code === 0) {
    form.value = res.data as never;
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as never);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as never);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as never);
    }
    console.log(form.value);
  } else {
    message.error("加载数据失败");
  }
};
onMounted(() => {
  loadData();
});
const form = ref({
  answer: "",
  content: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
  judgeConfig: {
    memoryLimit: 1000,
    stackLimit: 1000,
    timeLimit: 1000,
  },
  tags: ["简单"],
  title: "",
});
const onChange1 = (val: string) => {
  form.value.answer = val;
};
const onChange2 = (val: string) => {
  form.value.content = val;
};
const handleAdd = () => {
  form.value.judgeCase.push({
    input: "",
    output: "",
  });
};
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};

const doSubmit = async () => {
  //提交表单到后端
  //判断是要更新还是创建
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("更新成功");
    } else {
      message.error("更新失败," + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("创建成功");
    } else {
      message.error("创建失败," + res.message);
    }
  }
};
</script>
